package com.wxpay;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.example.lenovo.weiyi.wxapi.WXPayEntryActivity;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONException;
import org.json.JSONObject;

import nohttp.WaitDialog;
import share.Const;
import share.HttpIP;

public class WXPayHttp {
    private WaitDialog mWaitDialog;
    private IWXAPI msgApi;
    private PayReq req;
    Context context;
    private String prepay_id;
    private String nonceStr;
    private String timeStamp;
    private static WXPayHttp instance;

    public static WXPayHttp getInstance() {
        if (instance == null) {
            instance = new WXPayHttp();
        }
        return instance;
    }

    public void WeixinPay(Context context, String[] params) {

        this.context = context;
        req = new PayReq();
        msgApi = WXAPIFactory.createWXAPI(context, null);
        msgApi.registerApp(Constants.APP_ID);
        GetPrepayIdTask getPrepayId = new GetPrepayIdTask();
        getPrepayId.execute(params);

    }

    private class GetPrepayIdTask extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
//            mWaitDialog = new WaitDialog(context);
//            mWaitDialog.setCancelable(false);
//            mWaitDialog.setMessage("正在提交...");
//            mWaitDialog.show();
        }

        @Override
        protected void onPostExecute(String result) {
//            if (mWaitDialog != null) {
//                mWaitDialog.dismiss();
//            }
            try {
                JSONObject obj = new JSONObject(result);
                prepay_id = obj.getString("prepayId");
                nonceStr = obj.getString("nonceStr");
                timeStamp = obj.getString("timeStamp");
                Constants.APP_ID = obj.getString("appId");
                Constants.PARTNER_ID = obj.getString("partnerId");
                Constants.PARTNER_KEY = obj.getString("sign");
                sendPayReq();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected String doInBackground(String... params) {
            Request<String> mRequest = NoHttp.createStringRequest(HttpIP.Pay, Const.POST);
            mRequest.addHeader("token",params[0]);
            mRequest.add("payType", params[1]);
            mRequest.add("orderId", params[2]);
            Response<String> response = NoHttp.startRequestSync(mRequest);
            if (response.isSucceed()) {
                Log.i("onSucceed", "请求成功：\n" + response.get());
                try {
                    JSONObject obj = new JSONObject(response.get());
                    WXPayEntryActivity.wx_kcid = params[0];
                    if (obj == null) {
                        return "支付失败！";
                    }
                    return obj.getString("object");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return "支付失败！";
        }
    }

    private void sendPayReq() {
        req.appId = Constants.APP_ID;
        req.partnerId = Constants.PARTNER_ID;
        req.prepayId = prepay_id;
        req.packageValue = "Sign=WXPay";
        req.nonceStr = nonceStr;
        req.timeStamp = timeStamp;
        req.sign = Constants.PARTNER_KEY;
        msgApi.registerApp(Constants.APP_ID);
        msgApi.sendReq(req);
    }
}
